using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Entities;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class tbNhanVienDAL
    {
        private SqlConnection cn = null;
        public tbNhanVienDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public void Insert(tbNhanVienEntities NV)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhanVien_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenNV", NV.TenNV);
            cm.Parameters.AddWithValue("@Email", NV.Email);
            cm.Parameters.AddWithValue("@TaiKhoan", NV.TaiKhoan);
            cm.Parameters.AddWithValue("@MatKhau", NV.MatKhau);
            cm.Parameters.AddWithValue("@DiaChi", NV.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", NV.DienThoai);
            cm.ExecuteNonQuery();
        }

        public void Update(tbNhanVienEntities NV)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhanVien_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaNV", NV.MaNV);
            cm.Parameters.AddWithValue("@TenNV", NV.TenNV);
            cm.Parameters.AddWithValue("@Email", NV.Email);
            cm.Parameters.AddWithValue("@TaiKhoan", NV.TaiKhoan);
            cm.Parameters.AddWithValue("@MatKhau", NV.MatKhau);
            cm.Parameters.AddWithValue("@DiaChi", NV.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", NV.DienThoai);
            cm.ExecuteNonQuery();
        }

        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhanVien_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaNV", iD);
            cm.ExecuteNonQuery();
        }

        public Entities.tbNhanVienEntities.tbNhanVienColection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhanVien_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection",
            orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            Entities.tbNhanVienEntities.tbNhanVienColection NhanVienColection = new Entities.tbNhanVienEntities.tbNhanVienColection();
            while (dr.Read())
            {
                tbNhanVienEntities NV = new tbNhanVienEntities();
                NV.MaNV = int.Parse(dr.GetValue(0).ToString());
                NV.TenNV = dr.GetValue(1).ToString();
                NV.Email = dr.GetValue(2).ToString();
                NV.TaiKhoan = dr.GetValue(3).ToString();
                NV.MatKhau = dr.GetValue(4).ToString();
                NV.DiaChi = dr.GetValue(5).ToString();
                NV.DienThoai = dr.GetValue(6).ToString();
                NhanVienColection.Add(NV);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return NhanVienColection;
        }

        public Boolean KTNhanVien(string user, string pass)
        {
            string sql = "select count(*) from tbNhanVien where TaiKhoan='" + user + "' and MatKhau='" + pass + "'";
            SqlCommand cmd = new SqlCommand(sql, cn);
            int kq = (int)cmd.ExecuteScalar();
            if (kq > 0)
                return true;
            else
                return false;
        }
        public Boolean KTNhanVien2(string user)
        {
            string sql = "select count(*) from tbNhanVien where TaiKhoan='" + user + "'";
            SqlCommand cmd = new SqlCommand(sql, cn);
            int kq = (int)cmd.ExecuteScalar();
            if (kq > 0)
                return true;
            else
                return false;
        }
    }
}
